#include <iostream>
#include <cstring>

template<typename T>    //模板声明

T add(T a, T b)         //函数模板
{
    return a + b;
}

template<typename T>
void Swap(T &a, T &b)
{
    T temp = a;
    a = b;
    b = temp;
}

template<typename T>
void Sort(T * a, int size)
{
    for(int i = 0; i < size - 1; i ++)
    {
        for(int j = 0; j < size - i - 1; j++)
        {
            if(a[j] > a[j + 1])
            Swap<T>(a[j], a[j + 1]);
        }
    }
}

template<typename T>
void Print(T * a, int size)
{
    for(int i = 0; i < size; i++)
    {
        std::cout<<a[i]<<" ";
    }
    std::cout<<std::endl;
}

class Student
{
public:
    Student(int id, const std::string & name):id(id),name(name){};
    friend bool operator>(const Student & s1, const Student & s2);
    friend std::ostream & operator<<(std::ostream& os, const Student & s);

public:
    int id;
    std::string name;
};
bool operator>(const Student & s1, const Student & s2)
{
    return s1.id > s2.id;
}

std::ostream & operator<<(std::ostream& os, const Student & s)
{
    os<<s.id<<" "<<s.name<<std::endl;
    return os;
}

//模板的显式具体化
template<>
void Swap<Student>(Student & a, Student & b)
{
    std::string name = a.name;
    a.name = b.name;
    b.name = name;

}
int main()
{
    // //模板的显式调用
    // std::cout<<add<int>(1,2)<<std::endl;
    // //模板的隐式调用
    // printf("%d\n",add('\n','B'));   //\n的ascll码是10，B是66

    // int a[] = {3, 1, 2};
    //char a[] = {'c', 'a', 'b'};
    // std::string a[] = {"zhang", "lisi", "wang"};
    // Sort(a, 3);
    // Print(a, 3);

    // Student a[] = 
    // {
    //     {2, "zhangsan"},
    //     {1, "lisi"}
    // };
    // Sort(a, 2);
    // Print(a, 2);
    //隐式调用
    Student s1(1, "zhangsan");
    Student s2(2, "lisi");

    Swap<Student>(s1, s2);
    std::cout<<s1<<std::endl;
    std::cout<<s2<<std::endl;
    

    return 0;
}